package SocialMediaSentiment;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

import jxl.write.WriteException;

public class NaiveBayes {

	public static void main(String[] args) throws IOException, WriteException {

		float[] sentimentPos = getSentimentPos();
		String tweet;
		HashMap<String, String> toprint = new HashMap<String, String>();
		HashMap<String, Float> wordsPosPos = getWordsPos("CorrectedCounterTrainSetBiggerThanZero.xls");
		HashMap<String, Float> wordsPosNeg = getWordsPos("CorrectedCounterTrainSetLessThanZero.xls");
		HashMap<String, Float> wordsPosNeut = getWordsPos("CorrectedCounterTrainSetZero.xls");
		HashMap<Integer, String[]> tweetsWithSentiment = ReadExcel.read(
				"CorrectedTweets.xls", 0, 1);
		float total = getTotalCount("CorrectedCounterTrainSetZero.xls") + getTotalCount("CorrectedCounterTrainSetBiggerThanZero.xls") + getTotalCount("CorrectedCounterTrainSetLessThanZero.xls");
		Iterator<Integer> k = (Iterator<Integer>) tweetsWithSentiment.keySet()
				.iterator();
		String[] data;
		while (k.hasNext()) {
			boolean printer = false;
			data = tweetsWithSentiment.get(k.next());
			tweet = data[0];
			String sentiment = data[1];
			// System.out.println(tweet);
			float[] tweetpos = calcTweetPos(tweet, sentimentPos, wordsPosPos,
					wordsPosNeg, wordsPosNeut,  total);
			int classification = getClassification(tweetpos[0],
					tweetpos[1], tweetpos[2]);
			
			if (classification > 0
					&& Integer.parseInt(sentiment.replace(" ", "")) <= 0) {
				printer = true;
			} else if (classification == 0
					&& Integer.parseInt(sentiment.replace(" ", "")) != 0) {
				printer = true;
			} else if (classification < 0
					&& Integer.parseInt(sentiment.replace(" ", "")) >= 0) {
				printer = true;
			}

			if (printer == true) {
				toprint.put("" + tweet + "" + sentiment, "" + tweetpos[0] + "|"
						+ tweetpos[1] + "|" + tweetpos[2]);
				// System.out.println(sentimentPos[0] + " " + sentimentPos[1]
				// + " " + sentimentPos[2]);

				// System.out.println("Tweet: {" + tweet + "} Pos|Neg|Neut:"
				// + tweetpos[0] + "|" + tweetpos[1] + "|" + tweetpos[2]
				// + "||| " + data[1]);
			}
		}
		writeExcel.write("wrongBayesClass.xls", toprint);

	}

	public static int getClassification(float pos, float neg, float neut) {
		int classification = 1;
		if (neg > pos && neg > neut ) {
			classification = -1;
		} else if (neut > pos) {
			classification = 0;
		}
		return classification;

	}

	public static float[] calcTweetPos(String tweet, float[] sentimentPos,
			HashMap<String, Float> wordsPosPos,
			HashMap<String, Float> wordsPosNeg,
			HashMap<String, Float> wordsPosNeut, float total )
			throws IOException {
		float[] newSentimentPos = new float[3];
		newSentimentPos[0] = sentimentPos[0];
		newSentimentPos[1] = sentimentPos[1];
		newSentimentPos[2] = sentimentPos[2];
		HashMap<String, Float> wordsPos = new HashMap<String, Float>();
		for (int i = 0; i < 3; i++) {
			StringTokenizer tokenizer = new StringTokenizer(tweet);
			switch (i) {
			case (0):
				wordsPos = wordsPosPos;
				break;
			case (1):
				wordsPos = wordsPosNeg;
				break;
			case (2):
				wordsPos = wordsPosNeut;
				
			}
			while (tokenizer.hasMoreTokens()) {
				String word = tokenizer.nextToken();
				try {
					float wordpos = wordsPos.get(word);
					newSentimentPos[i] = newSentimentPos[i] * wordpos * 10000;
				} catch (NullPointerException e) {
					newSentimentPos[i] = newSentimentPos[i] * 1 / total * 10000;
				}
				// if(wordpos != null){

				// }

			}
		}
		// scale the possibilities
		float newSentimentPostotal = newSentimentPos[0] + newSentimentPos[1]
				+ newSentimentPos[2];
		for (int i = 0; i < newSentimentPos.length; i++) {
			newSentimentPos[i] = newSentimentPos[i] / newSentimentPostotal;
		}
		return newSentimentPos;
	}

	public static float getTotalCount(String file) throws IOException {
		float total = 0;
		HashMap<Integer, String[]> IndexWordsCounter = ReadExcel.read(file, 0,
				1);
		Iterator<Integer> k = (Iterator<Integer>) IndexWordsCounter.keySet()
				.iterator();
		// System.out.println(file);
		while (k.hasNext()) {
			int index = k.next();
			String[] WordsCounter = IndexWordsCounter.get(index);
			float count = Float.parseFloat(WordsCounter[1]);
			total += count;
			// System.out.println(WordsCounter[0] + "  " + WordsCounter[1]);
		}
		return total;
	}

	public static float[] getSentimentPos() throws IOException {
		float[] sentimentPos = new float[3];
		float pos = 0;
		float neg = 0;
		float neut = 0;
		float total = 0;

		HashMap<Integer, String[]> IndexTweets = ReadExcel.read(
				"CorrectedTweets.xls", 0, 1);
		Iterator<Integer> k = (Iterator<Integer>) IndexTweets.keySet()
				.iterator();

		while (k.hasNext()) {
			int index = k.next();
			String[] TweetSentiment = IndexTweets.get(index);
			int sentiment = Integer
					.parseInt(TweetSentiment[1].replace(" ", ""));
			if (sentiment > 0)
				pos++;
			else if (sentiment < 0)
				neg++;
			else
				neut++;
			total++;

		}
		sentimentPos[0] = pos / total;
		sentimentPos[1] = neg / total;
		sentimentPos[2] = neut / total;
		return sentimentPos;

	}

	public static HashMap<String, Float> getWordsPos(String file)
			throws IOException {
		double total = getTotalCount(file);
		HashMap<String, Float> wordsPos = new HashMap<String, Float>();
		HashMap<Integer, String[]> IndexWordsCounter = ReadExcel.read(file, 0,
				1);
		Iterator<Integer> k = (Iterator<Integer>) IndexWordsCounter.keySet()
				.iterator();

		while (k.hasNext()) {
			int index = k.next();
			String[] WordsCounter = IndexWordsCounter.get(index);
			String word = WordsCounter[0];
			float count = Float.parseFloat(WordsCounter[1]);
			wordsPos.put(word, (float) ((count+1) / total));
			int i = 0;
			// System.out.println(WordsCounter[0] + "  " + WordsCounter[1]);
		}

		return wordsPos;
	}

}
